<?php
namespace MyAdmin\Controller;

use Think\Controller;

class CommonController extends Controller
{
  protected $now_time;
  protected $admin_id;
  protected $app_id;
  protected $admin_info;
  protected $app_info;

  public function __construct()
  {
    parent::__construct();

    $this->now_time = gmtime();
    // $this->check_cookie();
  }

  protected function check_cookie()
  {
    //检测是否登录 token是否过期
    $this->admin_id = $_SESSION['admin_id'] ? $_SESSION['admin_id'] : 0;

    if (isset($_COOKIE['uautoadmin']) && !$this->admin_id) {
      $value = explode('|', encryption($_COOKIE['uautoadmin'], 1));

      //查看token是否一致
      $token = M("admins")->where("id='".$value[0]."'")->getField('token');
      if ($value[1] == $token) {
        $admin_info = D('Admins')->get_admininfo($value[0]);
        //检查用户名
        if ($admin_info['id']) {
          D("Admins")->set_admin_session($admin_info);
          //自动登录是否记录
        }
      }
    } elseif ($this->admin_id) {
      $admin_info = D('Admins')->get_admininfo($this->admin_id);
    }
    $this->admin_info = $admin_info;

    if ($admin_info['app_id']) {
      $this->app_id = $admin_info['app_id'];
      $this->app_info = D('Admins')->get_appinfo($admin_info['app_id']);
    }

    $this->assign("admin_info", $admin_info);
  }

  protected function check_login()
  {
    $app_id = $_SESSION['we7_acid'];

    if (!$app_id) {
      redirect('/web/index.php?c=wxapp&a=version&do=home');
    }

    $app = M('apps')->where("id={$app_id}")->find();
    if (!$app) {
      $data = array(
        'id' => $app_id,
        'name' => '禾匠微店大师');
      $result = M('apps')->data($data)->add();

      $result && $data['id'] = $app_id;
      $app = $data;
    }

    $this->app_id = $app_id;
    $this->app_info = $app;
    $this->assign('app_name', $app['name']);
    $this->assign("admin_info", array('name' => 'admin'));

    $msgs = M('messages')->where("app_id={$app_id} and status<2")->order('id desc')->limit(500)->select();
    foreach ($msgs as $key => &$value) {
      $value['created_at'] = local_time($value['created_at']);
      switch ($value['types']) {
        case 1:
          $value['types_str'] = '退款申请成功';
          $value['url'] = U("msgs/show?id={$value['id']}");
          break;
        case 2:
          $value['types_str'] = '余额提现申请';
          $value['url'] = U("msgs/show?id={$value['id']}");
          break;
        case 3:
          $value['types_str'] = '退款申请失败';
          $value['url'] = U("msgs/show?id={$value['id']}");
          break;
        case 4:
          $value['types_str'] = '平台订单通知';
          $value['url'] = U("msgs/show?id={$value['id']}");
          break;
        case 5:
          $value['types_str'] = '平台订单通知';
          $value['url'] = U("msgs/show?id={$value['id']}");
          break;
        case 6:
          $value['types_str'] = '返利提现申请';
          $value['url'] = U("msgs/show?id={$value['id']}");
          break;
        default:
          $value['types_str'] = '未知';
          break;
      }
    }
    $msg_counts = M('messages')->where("app_id={$app_id} and status=0")->count();
    $this->assign("msgs", array('list' => $msgs, 'count' => $msg_counts));

    check_dailytask();
  }
}
